﻿using System.Linq;
using Google.GData.Client;
using Google.Picasa;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Picasa
{
    public class PicasaHelper
    {
        public static MediaItem GetMediaItemById(string id, string username, string password)
        {
            var photo = GetPicasaPhoto(id, username, password);
            return photo.ToMediaItem();
        }

        public static string GetPhotoUrlById(string id, string username, string password)
        {
            var photo = GetPicasaPhoto(id, username, password);
            return photo.PhotoUri.ToString();
        }

        private static Photo GetPicasaPhoto(string id, string username, string password)
        {
            var photoId = id.Split(',')[0];
            var albumId = id.Split(',')[1];

            var requestSettings = new RequestSettings("Picasa", username, password);
            var picasaRequest = new PicasaRequest(requestSettings);

            var photos = picasaRequest.GetPhotosInAlbum(albumId);
            return photos.Entries.Where(entry => entry.Id == photoId).SingleOrDefault();
        }
    }
}